<template>
  <div>
    <van-nav-bar title="京东登录" left-text="返回" left-arrow @click-left="$router.go(-1)" />
    <van-form @submit="onSubmit" to='/register/yzm?tel=values'>
      <van-cell-group inset>
        <van-field v-model="tel" name="tel" label="请输入手机号" placeholder="" :rules="[{ pattern, message: '请输入正确内容' }]" />
      </van-cell-group>
      <div style="margin: 16px;">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang='ts'>
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import { Dialog } from 'vant';
import { sfzhu } from '@/api/sfzhu'
import { useUserStore } from '@/store/userstore';
const useStore = useUserStore();
const router = useRouter()
const tel = ref('')
const pattern = /^(?:(?:\+|00)86)?1[3-9]\d{9}$/;
console.log(1, useStore);
const onSubmit = (values: {
  tel: string
}) => {
  console.log(values);
  // 存到pinia里面
  useStore.userId = tel.value

  sfzhu(values).then((res: any) => {
    console.log(values);
    console.log(res);
    if (res.code == '200') {
      Dialog.confirm({
        title: '标题',
        message:
          '是否发送验证码',
      })
        .then(() => {
          // on confirm
          router.push('/register/yzm')
          // useStore.setUserId(values.userid)
        })
        .catch(() => {
          // on cancel
        });

    } else if (res.code == '10005') {
      Dialog.confirm({
        title: '标题',
        message:
          '已注册',
      })
    }
  })

}
console.log(router);

</script>
<style lang="scss" scoped>
.van-field {
  border-bottom: 1px solid #ccc;
}
</style>